import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * Creater with IntelliJ IDEA.
 * Description:
 * User: 86152
 * Dae: 2022-08-03
 * Time: 9:21
 */
public class demo1 {
    public static void main(String[] args) {
        Find();
    }
    public static void Find() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的根目录");
        String root = scanner.next();
        File fileRoot = new File(root);
        //检测是否是目录，不是就return
        if(!fileRoot.isDirectory()) {
            System.out.println("该文件不是目录或目录不存在，退出");
            return;
        }
        System.out.println("请输入要查找的文件名");
        String aim = scanner.next();
        //是就进行扫描
        ArrayList<File> ret = new ArrayList<>();
        scanFile(fileRoot,aim,ret);
        System.out.println("找到以下文件：");
        for(File file:ret) {
            System.out.println(file.getName()+"   "+file.getAbsoluteFile());
            System.out.println("是否删除(Y/N)");
            String choice = scanner.next();
            if(choice.compareTo("Y")==0) {
                file.delete();
                System.out.println("已删除该文件");
            } else {
                continue;
            }

        }
    }
    public static void scanFile(File fileRoot,String aim,ArrayList<File> ret) {
        File[] list = fileRoot.listFiles();//库函数读取该目录下的所有文件
        if(list.length==0) {
            return;
        }
        //依次取出目录文件
        for(File file:list) {
            //检测是不是目录
            if(file.isDirectory()) {
                scanFile(file,aim,ret);
            } else {
                if(file.getName().contains(aim)) {
                    ret.add(file.getAbsoluteFile());
                }
            }
        }
    }
}
